﻿using PetaPoco;

namespace BlazorAdmin.Extensions
{
    //
    // 摘要:
    //     PetaPoco 批量插入扩展操作类
    public static class IDatabaseInsertBatchExtensions
    {
        /// <summary>
        /// 批量插入数据库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch<T>(this IDatabase db, IEnumerable<T> pocos)
        {
            if (pocos.Any())
            {
                BatchProvider.Create(db).InsertBatch(pocos);
            }
        }

        /// <summary>
        /// 批量插入数据到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, IEnumerable<object> pocos)
        {
            db.InsertBatch(tableName, "", autoIncrement: false, pocos);
        }

        /// <summary>
        /// 批量插入数据到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="primaryKeyName"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, string primaryKeyName, IEnumerable<object> pocos)
        {
            db.InsertBatch(tableName, primaryKeyName, autoIncrement: false, pocos);
        }

        /// <summary>
        /// 批量插入数据到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="primaryKeyName"></param>
        /// <param name="autoIncrement"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, string primaryKeyName, bool autoIncrement, IEnumerable<object> pocos)
        {
            if (pocos.Any())
            {
                BatchProvider.Create(db).InsertBatch(tableName, primaryKeyName, autoIncrement, pocos);
            }
        }
    }
}
